/*
 *
 * SimplePCR1000 (Qt4) Version 1.x
 * Copyright (C) 2011  Tyler H. Jones (me@tylerjones.me)
 * http://www.thebasementserver.com/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */


#include "common.h"

Common::Common() //Constructor
{
    ModeValues.insert("LSB", "00");
    ModeValues.insert("USB", "01");
    ModeValues.insert("AM", "02");
    ModeValues.insert("CW", "03");
    ModeValues.insert("FM", "05");
    ModeValues.insert("WFM", "06");

    FilterComboBox.append("3KHz");
    FilterComboBox.append("6KHz");
    FilterComboBox.append("15KHz");
    FilterComboBox.append("50KHz");
    FilterComboBox.append("230KHz");

    ModeComboBox.append("AM");
    ModeComboBox.append("CW");
    ModeComboBox.append("LSB");
    ModeComboBox.append("USB");
    ModeComboBox.append("FM");
    ModeComboBox.append("WFM");

    FilterValues.insert("3KHz", "00");
    FilterValues.insert("6KHz", "01");
    FilterValues.insert("15KHz", "02");
    FilterValues.insert("50KHz", "03");
    FilterValues.insert("230KHz", "04");

    StepSizeValues.insert("10Hz", 0.00001);
    StepSizeValues.insert("100Hz", 0.0001);
    StepSizeValues.insert("500Hz", 0.0005);
    StepSizeValues.insert("1KHz", 0.001);
    StepSizeValues.insert("5KHz", 0.005);
    StepSizeValues.insert("10KHz", 0.01);
    StepSizeValues.insert("50KHz", 0.05);
    StepSizeValues.insert("100KHz", 0.10);
    StepSizeValues.insert("500KHz", 0.5);
    StepSizeValues.insert("1MHz", 1.0);
    StepSizeValues.insert("10MHz", 10.0);

    DataRates.append("300");
    DataRates.append("1200");
    DataRates.append("4800");
    DataRates.append("9600"); //Default
    DataRates.append("19200");
    DataRates.append("38400");

#ifdef Q_WS_WIN
    ComPortValues.append("COM1");
    ComPortValues.append("COM1");
    ComPortValues.append("COM2");
    ComPortValues.append("COM3");
    ComPortValues.append("COM4");
    ComPortValues.append("COM5");
    ComPortValues.append("COM6");
    ComPortValues.append("COM7");
    ComPortValues.append("COM8");
    ComPortValues.append("COM9");
    ComPortValues.append("COM10");
    ComPortValues.append("COM11");
    ComPortValues.append("COM12");
    ComPortValues.append("Custom...");
#else
    ComPortValues.append("/dev/ttyS0");
    ComPortValues.append("/dev/ttyS1");
    ComPortValues.append("/dev/ttyS2");
    ComPortValues.append("/dev/ttyS3");
    ComPortValues.append("/dev/ttyS4");
    ComPortValues.append("/dev/ttyS5");
    ComPortValues.append("/dev/ttyS6");
    ComPortValues.append("/dev/ttyS7");
    ComPortValues.append("/dev/ttyS8");
    ComPortValues.append("/dev/ttyUSB0");
    ComPortValues.append("/dev/ttyUSB1");
    ComPortValues.append("/dev/ttyUSB2");
    ComPortValues.append("/dev/ttyUSB3");
    ComPortValues.append("/dev/ttyUSB4");
    ComPortValues.append("/dev/ttyUSB5");
    ComPortValues.append("/dev/ttyUSB6");
    ComPortValues.append("/dev/ttyUSB7");
    ComPortValues.append("/dev/ttyUSB8");
    ComPortValues.append("Custom...");
#endif

}

Common::~Common() //Deconstructor
{
    ComPortValues.clear();
    StepSizeValues.clear();
    FilterValues.clear();
    ModeValues.clear();
}

QString Common::qvar(QVariant var) {
    return QString(var.toString());
}

int Common::b2n(bool b) {
    if(b) { return 1; } else { return 0; }
}

bool Common::n2b(int n) {
    if(n == 1) { return true; } else { return false; }
}

QString Common::PadFreq(QString freq) {
    if(freq.startsWith(".")) { freq.prepend("0"); }
    if(freq.endsWith(".")) { freq.append("0"); }
    if(!freq.contains(".")) { freq.append(".0"); }
    QStringList sfreq = freq.split(".", QString::KeepEmptyParts);
    QString GMMM = "0000" + sfreq[0];
    QString KKKHHH = sfreq[1] + "000000";
    qDebug() << "Freq GMMM raw = " << GMMM;
    qDebug() << "Freq KKKHHH raw = " << KKKHHH;
    GMMM = GMMM.remove(0, GMMM.length() - 4);
    KKKHHH.chop(KKKHHH.length() - 6);
    qDebug() << "Freq GMMM trimmed = " << GMMM;
    qDebug() << "Freq KKKHHH trimmed = " << KKKHHH;
    return GMMM + "." + KKKHHH;
}
